package com.jkchen.controller;

import com.github.pagehelper.PageInfo;
import com.jkchen.pojo.User;
import com.jkchen.pojo.query.UserQuery;
import com.jkchen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/")
    public String index(Model model, UserQuery userQuery){
        PageInfo<User> userPageInfo = userService.findByName(userQuery);
        model.addAttribute("page",userPageInfo);
        return "index";
    }
    @PostMapping("/")
    public String findByName(Model model,UserQuery userQuery){
        PageInfo<User> byName = userService.findByName(userQuery);
        model.addAttribute("page",byName);
        return "index";
    }

    @GetMapping("/delete/{id}")
    public String deleUserById(@PathVariable("id")int id, RedirectAttributes attributes){
        boolean b = userService.deleUserById(id);
        if (b){
            attributes.addFlashAttribute("message","删除成功");
            return "redirect:/";//返回重定向
        }else{
            attributes.addFlashAttribute("message","删除失败");
            return "redirect:/";
        }
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id,Model model){
        User byId = userService.findById(id);
        model.addAttribute("user",byId);
        return "edit";
    }
    @PostMapping("/edit")
    public String editUpdate(User user,RedirectAttributes attributes){

        if(user.getId()!=null){
            boolean userByName = userService.findUserByName(user.getName());
            if(userByName){
                boolean b = userService.UpdateUserById(user);
                if (b){
                    attributes.addFlashAttribute("message","修改成功");
                    return "redirect:/";//返回重定向
                }else{
                    attributes.addFlashAttribute("message","修改失败");
                    return "redirect:/";
                }
            }else{
                attributes.addFlashAttribute("message","用户名重复添加失败");
                return "redirect:/edit/"+user.getId();
            }
        }else{
            boolean userByName = userService.findUserByName(user.getName());
            if(userByName){
                boolean b = userService.insertUser(user);
                if(b){
                    attributes.addFlashAttribute("message","添加成功");
                    return "redirect:/";
                }else{
                    attributes.addFlashAttribute("message","添加失败");
                    return "redirect:/";
                }
            }else{
                attributes.addFlashAttribute("message","用户名已存在");
                return "redirect:/";
            }
        }

    }

    @GetMapping ("/insert")
    public String insert(Model model){
        User user=new User();
        model.addAttribute("user",user);
        return "edit";
    }
}
